/**
******************************************************************************
* 文件名程: main.c 
* 作    者: 东莞-邓凯歌 微信:D13631760681
* 功    能: 主程序
* 硬    件: PY32F403C_KFB_V0.1
* 说明：模拟256个软件定时器
* 淘宝店铺:https://inj0e1y0ystqot8fekw6mazpf2lxj6.taobao.com  摸鱼9块9电子模块
* 软件版本 V1.0.1(2025/2/22 9:02)
******************************************************************************
**/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"
#include "ntc.h"

#include "soft_timer.h"

uint8_t aRxBuffer,bRxBuffer;
int32_t acc_adcval,mean_value;
int16_t tempval;

static void APP_SystemClockConfig(void);

void timer1_printf()
{
	printf("timer1 time up\r");
}

void timer2_printf()
{
	printf("timer2 once\r");
}
/******************************************************************************
**函数信息 ：main()           
**功能描述 ：主函数
**输入参数 ：无
**输出参数 ：无
**说    明 : 
******************************************************************************/		
int main(void)
{
	HAL_Init();

	APP_SystemClockConfig();
	APP_GPIO_Init();
	APP_DMA_Init();
	APP_ADC1_Init();
	APP_USART1_UART_Init();
	
	while (1)
	{
		
//		HAL_Delay(500);
//		printf("ADC_VALUE = %d,   NTC_VALUE = %d\r\n",mean_value, tempval);  	//串口打印ADC数值和NTC温度值
		soft_timer_enable(0,500);
		if( sptime[0].time_up)
		{
//			printf("ADC_VALUE = %d,   NTC_VALUE = %d\r\n",mean_value, tempval);  	//串口打印ADC数值和NTC温度值
			HAL_GPIO_TogglePin(LED0_PORT, LED0_PIN);
			HAL_GPIO_TogglePin(LED1_PORT, LED1_PIN);
			sptime[0].cur_time = 0;
		}
		
		soft_timer_cycle(1,1000,timer1_printf);
		soft_timer_single(2,500,timer2_printf);
	}
}

/******************************************************************************
**函数信息 : APP_SystemClockConfig(void)                
**功能描述 ：系统时钟配置
**输入参数 ：无
**输出参数 ：无
**说    明 : 
******************************************************************************/
static void APP_SystemClockConfig(void)
{
	RCC_OscInitTypeDef  OscInitstruct = {0};
	RCC_ClkInitTypeDef  ClkInitstruct = {0};

	OscInitstruct.OscillatorType  = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE | 
								  RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSI48M;
	OscInitstruct.HSEState        = RCC_HSE_ON;                               /* Enable HSE */
	OscInitstruct.HSEFreq         = RCC_HSE_16_32MHz;                         /* HSE working frequency range: 16M~32M */
	OscInitstruct.HSI48MState     = RCC_HSI48M_OFF;                           /* Disable HSI48M */
	OscInitstruct.HSIState        = RCC_HSI_ON;                               /* Enable HSI */
	OscInitstruct.LSEState        = RCC_LSE_OFF;                              /* Disable LSE */
	OscInitstruct.LSIState        = RCC_LSI_OFF;                              /* Disable LSI */
	OscInitstruct.PLL.PLLState    = RCC_PLL_ON;                              /* Disable PLL */
	OscInitstruct.PLL.PLLSource   = RCC_PLLSOURCE_HSE;                    /* PLL clock source: HSE */
	OscInitstruct.PLL.PLLMUL      = RCC_PLL_MUL7;                         /* PLL multiplication factor: 6 */
	/* Configure Oscillators */
	if(HAL_RCC_OscConfig(&OscInitstruct) != HAL_OK)
	{
		Error_Handler();
	}

	ClkInitstruct.ClockType       = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
	ClkInitstruct.SYSCLKSource    = RCC_SYSCLKSOURCE_PLLCLK;                 /* SYSCLK source select as HSE */
	ClkInitstruct.AHBCLKDivider   = RCC_SYSCLK_DIV1;                      /* AHB clock not divided */
	ClkInitstruct.APB1CLKDivider  = RCC_HCLK_DIV1;                        /* APB1 clock not divided */
	ClkInitstruct.APB2CLKDivider  = RCC_HCLK_DIV2;                        /* APB1 clock divided by 2 */
	/* Configure Clocks */
	if(HAL_RCC_ClockConfig(&ClkInitstruct, FLASH_LATENCY_6) != HAL_OK)
	{
		Error_Handler();
	}
}

/******************************************************************************
**函数信息 : ADC_DMATransferCompleteCallback(void)                
**功能描述 ：ADC中断回调
**输入参数 ：无
**输出参数 ：无
**说    明 : 
******************************************************************************/
void ADC_DMATransferCompleteCallback()
{
	uint8_t i;
	acc_adcval = 0;
	for( i=0; i<128; i++)
	{
		acc_adcval += ADC1_Datas[i];
	}
	mean_value = acc_adcval >>7;		//求128次ADC采样的平均值,用移位比除法运算更高效
	tempval =  ADC_Get_TempValue( mean_value);
}


/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
void Error_Handler(void)
{
  /* Infinite loop */
  while (1)
  {
  }
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     for example: printf("Wrong parameters value: file %s on line %d\r\n", file, line)  */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT Puya *****END OF FILE******************/
